package com.fanhao.dllo.justforyou.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.fanhao.dllo.justforyou.R;
import com.fanhao.dllo.justforyou.activity.NewsHolder01InforActivity;
import com.fanhao.dllo.justforyou.activity.NewsWebViewActivity;
import com.fanhao.dllo.justforyou.adapter.NewsRecyclerAdapter;
import com.fanhao.dllo.justforyou.adapter.NewsViewPageAdapter;
import com.fanhao.dllo.justforyou.entity.NewsRecyclerResult;
import com.fanhao.dllo.justforyou.entity.NewsViewPageResult;
import com.fanhao.dllo.justforyou.volley.VolleySingleton;
import com.google.gson.Gson;

import java.util.List;


public class NewsFragment extends Fragment implements NewsRecyclerAdapter.OnNewsRecyItemClickListener {
    private ViewPager viewPager;
    private NewsViewPageAdapter newsViewPageAdapter;

    private RecyclerView recyclerView;
    private NewsRecyclerAdapter newsRecyclerAdapter;


    VolleySingleton volleySingleton = VolleySingleton.getInstance();

    private Handler handler;
    private int currentItem = 0;
    private int maxItem = 0;

    private Runnable r = new Runnable() {
        @Override
        public void run() {
            // 设置当前左右滑动组件的小标为1,即第二张图片
            currentItem++;
            viewPager.setCurrentItem(currentItem % maxItem);
            handler.postDelayed(r, 3000);
        }
    };

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_news, null);

        return v;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        viewPager = (ViewPager) view.findViewById(R.id.viewPage);
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);



    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initOther();
        initData();
    }

    private void initView() {

        // 构建适配器对象
        newsViewPageAdapter = new NewsViewPageAdapter(getFragmentManager());
        viewPager.setAdapter(newsViewPageAdapter);

        // recyclerview绑定适配器

        GridLayoutManager gm = new GridLayoutManager(getActivity(), 1);
        recyclerView.setLayoutManager(gm);

        newsRecyclerAdapter = new NewsRecyclerAdapter(getActivity());
        recyclerView.setAdapter(newsRecyclerAdapter);
        newsRecyclerAdapter.setListener(this);


    }

    private void initOther() {

        String url = "http://c.3g.163.com/nc/ad/headline/android/0-3.html";
        StringRequest request= new StringRequest(url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Gson gson = new Gson();
                        NewsViewPageResult nvpr = gson.fromJson(response,
                                NewsViewPageResult.class);

                        if (nvpr != null) {
                            List<NewsViewPageResult.HeadlineAdEntity> headline_ads =
                                    nvpr.getHeadline_ad();
                            if (headline_ads != null && headline_ads.size() > 0) {
                                Log.i("fanhao", "" + headline_ads);
                        newsViewPageAdapter.addData(headline_ads);
                         }
                        }

                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.printStackTrace();

                    }
                });


        volleySingleton.addRequest(request);
    }

    private void initData() {

            String url = "http://c.3g.163.com/nc/article/headline/T1348647909107/0-20.html";
            StringRequest request= new StringRequest(url,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            Gson gson = new Gson();
                            NewsRecyclerResult nr = gson.fromJson(response,
                                    NewsRecyclerResult.class);

                            if (nr != null) {

                                List<NewsRecyclerResult.T1348647909107Entity> t1348647909107Entity =
                                        nr.getT1348647909107();
                                if (t1348647909107Entity != null && t1348647909107Entity.size() > 0) {
                                    newsRecyclerAdapter.addData(t1348647909107Entity);
                                    Log.d("NewsFragment", "t1348647909107Entities:" + t1348647909107Entity);


                                }
                            }

                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            error.printStackTrace();

                        }
                    });


            volleySingleton.addRequest(request);
        }


    @Override
    public void recyItemClickListener(NewsRecyclerResult.T1348647909107Entity nr) {
        if (nr.getUrl_3w() != null) {

        Intent intent = new Intent(getContext(), NewsWebViewActivity.class);
        intent.putExtra("newsWebView",nr.getUrl_3w());
        startActivity(intent);
//        Toast.makeText(getActivity(), nr.getUrl_3w(), Toast.LENGTH_SHORT).show();

        } else {
            String[] data = nr.getSkipID().split("\\|");
            if (data != null) {
                String id = data[1];

//           id = nr.getSkipID().substring(nr.getSkipType().length() - 5, nr.getSkipType().length());
            Log.d("NewsFragment", id);
            Intent intent = new Intent(getContext(), NewsHolder01InforActivity.class);
            intent.putExtra("id",id);
            startActivity(intent);
        }
        }
    }
}
